package work.atpgroup.controller;



import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import work.atpgroup.entity.User;
import work.atpgroup.entity.Want;
import work.atpgroup.entity.WantValidator;
import work.atpgroup.service.HashPassword;
import work.atpgroup.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@SessionAttributes(names={"user"},types = {User.class})
public class UserController {
    @InitBinder("want")
    public void initBinder(WebDataBinder binder){
        binder.replaceValidators(new WantValidator());
    }
    @Resource(name="userService")
    private UserService userService;
    @Resource(name="hashPassword")
    private HashPassword hashPassword;
    @RequestMapping("/")
    public String context(){
        return "public/index";
    }
    @GetMapping("/index")
    public String context_index(){
        return "public/index";
    }
    @GetMapping("/login")
    public String redirectToLoginView(){
        return "public/login";
    }
    @GetMapping("/register")
    public String redirectToRegistView(){
        return "public/register";
    }
    @GetMapping("/resetPassword")
    public String redirectToResetPassword(){
        return "public/resetPassword";
    }
    @RequestMapping("/loginAction")
    public String loginAction(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpServletResponse response, HttpServletRequest request){
        Long hashedPassword = hashPassword.createKey(password);
        if(userService.login(username,hashedPassword)>0){
            model.addAttribute("tip","登陆成功");
            User user = userService.getUserByComplexNameOrId(username);
            model.addAttribute("user",user);
            Cookie cookie = new Cookie("username",username);
            Cookie cookie2 = new Cookie("password",hashedPassword+"");
            cookie.setMaxAge(24*3600*3);
            cookie2.setMaxAge(24*3600*3);
            response.addCookie(cookie);
            response.addCookie(cookie2);
            System.out.println(request.getContextPath());
            System.out.println(request.getRequestURI());
//             在网页中利用cookie实现找到cookie中的用户密码，cookie保存3天
            return "redirect:index";
        }else{
            model.addAttribute("tip","登陆失败");
            return "test/success";
        }
    }
    @RequestMapping("/success")
    public String success(){
        return "test/success";
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, SessionStatus status){
        status.setComplete();
        return "redirect:index";
    }
    @RequestMapping("/control")
    public String control(){
        return "test/success";
    }
    @RequestMapping("/registerAction")
    public String registerAction(@Validated Want want, Errors errors, Model model, HttpServletResponse response, HttpServletRequest request){
        if(errors.hasErrors()){
            return "public/register";
        }
        User user = new User();
        user.setUsername(want.getUsername());
        user.setNickname(want.getNickname());
        user.setPassword(hashPassword.createKey(want.getPassword()));
        user.setSchoolId(want.getSchoolId());
        int x = 0;
        if(userService.getUserCountByComplexName(want.getUsername())>=1){
            model.addAttribute("sameUsername",want.getUsername());
            x++;
        }
        if(userService.getUserCountByComplexName(want.getNickname())>=1){
            model.addAttribute("sameNickname",want.getNickname());
            x++;
        }
        if(userService.getUserCountByComplexName(want.getSchoolId())>=1){
            model.addAttribute("sameSchoolId",want.getSchoolId());
            x++;
        }
        if(x>0){
            model.addAttribute("same","1");
            return "public/register";
        }
        userService.insertUser(user);
        user.setUserId(userService.getUserByComplexNameOrId(user.getUsername()).getUserId());
        model.addAttribute("tip","登陆成功");
        model.addAttribute("user",user);
        Cookie cookie = new Cookie("username",user.getUsername());
        Cookie cookie2 = new Cookie("password",user.getPassword()+"");
        cookie.setMaxAge(24*3600*3);
        cookie2.setMaxAge(24*3600*3);
        response.addCookie(cookie);
        response.addCookie(cookie2);
        System.out.println(request.getContextPath());
        System.out.println(request.getRequestURI());
        return "public/index";
    }
}
